/**
 * 
 */
package org.swing.utility.jai.support;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author Quynh Nhu
 *
 */
public class ImageToByte {
	  public static byte[] imageToByteArray(Image image) throws IOException {
		    // get image size
		    int width = image.getWidth(null);
		    int height = image.getHeight(null);
		    
		    // store image data as raw int values
		    try {
		      int[] imageSource = new int[width * height];
		      PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, imageSource, 0, width);
		      pg.grabPixels();
		      
		      // zip data
		      ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
		      GZIPOutputStream zippedStream = new GZIPOutputStream(byteStream);
		      ObjectOutputStream objectStream = new ObjectOutputStream(zippedStream);
		      objectStream.writeShort(width);
		      objectStream.writeShort(height);
		      objectStream.writeObject(imageSource);
		      objectStream.flush();
		      objectStream.close();
		      return byteStream.toByteArray();
		    }
		    catch (Exception e) {
		      throw new IOException("Error storing image in object: " + e);
		    }
		  }
		  
		  /**
		   * Converts a byte array to an image that has previously been converted with imageToByteArray(Image image).
		   * 
		   * @param data  The image.
		   * @return  The image.
		   */
		  public static Image byteArrayToImage(byte[] data) {
		    try {
		      // unzip data
		      ByteArrayInputStream byteStream = new ByteArrayInputStream(data);
		      GZIPInputStream zippedStream = new GZIPInputStream(byteStream);
		      ObjectInputStream objectStream = new ObjectInputStream(zippedStream);
		      int width = objectStream.readShort();
		      int height = objectStream.readShort();
		      int[] imageSource = (int[])objectStream.readObject();
		      objectStream.close();

		      // create image
		      MemoryImageSource mis = new MemoryImageSource(width, height, imageSource, 0, width);
		      return Toolkit.getDefaultToolkit().createImage(mis);    
		    }
		    catch (Exception e) {
		      return null;
		    }
		  }
}
